﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace ElingOA6_Admin_DSJCJ.Models
{
    public class TreeModelList : List<TreeItemModel>, IHierarchicalEnumerable
    {
        public TreeModelList()
        {
        }

        public TreeModelList(IEnumerable<TreeItemModel> libraryTreeItemModels)
        {
            if (libraryTreeItemModels == null)
            {
                throw new ArgumentNullException("libraryTreeItemModels");
            }

            AddRange(libraryTreeItemModels);
        }

        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            return enumeratedItem as IHierarchyData;
        }
    }

     public class TreeItemModel : IHierarchyData
    {
        //public string Id { get; set; }
        //public string Name { get; set; }
        //public string ParentId { get; set; }

        public string Id { get; set; }
        public String Name { get; set; }
        public TreeItemModel Parent { get; protected set; }
        public TreeModelList Children { get; protected set; }
        public string ImageUrl { get; set; }
        public string Target { get; set; }


        public TreeItemModel(TreeItemModel parent)
        {
            Parent = parent;
            if (Parent != null)
                Parent.Children.Add(this);
            Children = new TreeModelList();
        }

        public IHierarchicalEnumerable GetChildren()
        {
            return Children;
        }

        public IHierarchyData GetParent()
        {
            return Parent;
        }

        public bool HasChildren
        {
            get { return Children.Count > 0; }
        }

        public string Path
        {
            get
            {
                TreeItemModel treeItemModel = (TreeItemModel)GetParent();

                string path = Id.ToString();

                while (treeItemModel != null)
                {
                    path = String.Format("{0}/{1}", treeItemModel.Id, path);

                    treeItemModel = (TreeItemModel)treeItemModel.GetParent();
                }

                return path;
            }
        }

        public object Item
        {
            get { return this; }
        }

        public string Type
        {
            get { return typeof(TreeItemModel).ToString(); }
        }

    }
}